﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;

namespace MINERP.BLL.Statistics
{

    public class SalesStatBll
    {
        public delegate void ShowMessage(string str);//定义委托将处理信息传递到表示层
        public delegate void ShowProcess(int currInt);//定义委托将处理进度传递到表示层

        public event ShowMessage ShowMsgHandle;
        public event ShowProcess ShowCurrPointHandle;
       
        public List<ShopStatsInfo> GetStatsProduct(List<int> listShopId, DateTime inTimeStart, DateTime inTimeEnd)
        {
            List<ShopStatsInfo> rtnList = new List<ShopStatsInfo>();
            ShowMsgHandle("准备数据");
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                int iCount = 0;
                ShowCurrPointHandle(iCount);
                foreach (int shopid in listShopId)
                {
                    iCount++;
                    Shop currshop = db.Shop.FirstOrDefault(sh => sh.ShopId == shopid);                  
                    List<SHOP_Bill> currSB = db.SHOP_Bill.Where(s => s.Shop.ShopId == shopid && s.CreateTime < inTimeEnd && s.CreateTime > inTimeStart).ToList();
                    ShowMsgHandle("获取到商户" + currshop.shopName + "的信息");
                    var GroupDateSb = currSB.GroupBy(p=>p.CreateTime);
        //UnNeedStatArea,//不需要统计面积
        //NeedStatArea,//需要统计面积,可以用来判断是否为输出类型
        //Design//设计类收费
                   
                    foreach (var oneDay in GroupDateSb)
                    {
                        ShopStatsInfo currssi = new ShopStatsInfo();
                        currssi.ShopID = currshop.ShopId;
                        currssi.ShopName = currshop.shopName;

                        double printAreas = 0;
                        double printMoney = 0;
                        double ProductMoney = 0;
                        double OtherMoney = 0;
                        //double allMoney = 0;
                        currssi.ProductDate = oneDay.Key.Date;
                        ShowMsgHandle("正在分析商户" + currshop.shopName + "的" + oneDay.Key.Date + "信息");
                        //printAreas=oneDay.ToList().Sum(s=>s.Bill_Detail.Sum(su=>su.Bill_Detail_Detail.Where(bdd => bdd.Type == 1).Sum(sum=>sum.Area)));
                        //printMoney = oneDay.ToList().Sum(s => s.Bill_Detail.Sum(su => su.Bill_Detail_Detail.Where(bdd => bdd.Type == 1).Sum(sum => sum.Count)));
                        //ProductMoney = oneDay.ToList().Sum(s => s.Bill_Detail.Sum(su => su.Bill_Detail_Detail.Where(bdd => bdd.Type == 0).Sum(sum => sum.Count)));
                        //OtherMoney = oneDay.ToList().Sum(s => s.Bill_Detail.Sum(su => su.Bill_Detail_Detail.Where(bdd => bdd.Type == 2).Sum(sum => sum.Count)));

                        foreach (SHOP_Bill sb in oneDay.ToList())
                        {

                            if (!sb.Bill_Detail.IsLoaded)
                            {
                                sb.Bill_Detail.Load();
                            }

                            foreach (Bill_Detail bd in sb.Bill_Detail)
                            {
                                if (!bd.Bill_Detail_Detail.IsLoaded)
                                {
                                    bd.Bill_Detail_Detail.Load();
                                }
                                printAreas += bd.Bill_Detail_Detail.Where(bdd => bdd.Type == 1).Sum(p => p.Area);
                                printMoney += bd.Bill_Detail_Detail.Where(bdd => bdd.Type == 1).Sum(p => p.Count);
                                ProductMoney += bd.Bill_Detail_Detail.Where(bdd => bdd.Type == 0).Sum(p => p.Count);
                                OtherMoney += bd.Bill_Detail_Detail.Where(bdd => bdd.Type == 2).Sum(p => p.Count);
                                //allMoney = allMoney + OtherMoney + printMoney + ProductMoney;

                            }


                        }
                        currssi.PrintAreas = printAreas;
                        currssi.PrintMoney = printMoney;
                        currssi.ProductMoney = ProductMoney;
                        currssi.OtherMoeny = OtherMoney;
                        currssi.AllMoney = printMoney + ProductMoney + OtherMoney;
                        rtnList.Add(currssi);
                    }
                    
                }

            }
            return rtnList;
        }

    }
    public class ShopStatsInfo
    {
        public int ShopID { get; set; }
        public string ShopName { get; set; }
        public double PrintAreas { get; set; }
        public double PrintMoney { get; set; }
        public double ProductMoney { get; set; }
        public double OtherMoeny { get; set; }
        public double AllMoney { get; set; }
        public DateTime ProductDate
        {
            get;
            set;


        }
    }
}
